<%@include file="../template/configuration.jsp" %>
<%@page import="models.Course"%>
<%@page import="classes.Valuta"%>
<%@page import="java.util.List"%>
<%@page import="classes.HibernateUtil"%>
<%@page import="dao.CourseDao"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%
if (request.getParameter("category_id") != null && request.getParameter("page") != null) {
    HibernateUtil hu = new HibernateUtil();
    CourseDao cd = new CourseDao(hu.openSession());
    List<Course> courses = cd.getPagination(Long.parseLong(request.getParameter("category_id")), Integer.parseInt(request.getParameter("page")), 15);
    pageContext.setAttribute("courses", courses);
}
%>
<table class="overview">
    <tr>
        <th>${uq_course}</th>
        <th>${uq_description}</th>
        <th>${uq_price}</th>
        <th>${uq_teachers}</th>
        <th class="date">${uq_added}</th>
        <th class="manage">${uq_manage}</th>
    </tr>
    <c:set var="i" value="0"/>
    <c:forEach items="${courses}" var="course">
        <c:set var="i" value="${i + 1}"/>
        <tr class="<c:choose><c:when test="${i % 2 == 0}">even</c:when><c:otherwise>odd</c:otherwise></c:choose>">
            <td>${course.name}</td>
            <td>
                <c:choose>
                    <c:when test="${fn:length(course.description) > 40}">
                         <c:out value="${fn:substring(course.description, 0, 40)}"/>...
                    </c:when>
                    <c:otherwise>
                        ${course.description}
                    </c:otherwise>
                </c:choose>
            </td>
            <td>
                <c:set var="price" value="${course.price}" />
                <%=Valuta.getSignFromCurrency(pageContext.getAttribute("currency").toString())%> 
                <%=Valuta.calculateToCurrency(pageContext.getAttribute("currency").toString(), (Double) Double.parseDouble(pageContext.getAttribute("price").toString().replaceAll(",", ".")))%>
            </td>
            <td>
                <img src="${url}images/question.png" class="question" />
                <div class="relative">
                    <div class="tooltip">
                        <c:forEach items="${course.teacher}" var="teacher">
                             ${teacher.firstname}
                             ${teacher.insertion}
                             ${teacher.lastname}<br/>
                         </c:forEach>
                    </div>
                </div>
            </td>
            <td>${course.date}</td>
            <td>
                <a href="ManageCourse/form?idedit=${course.id}"><img src="${url}images/edit.png" /></a> 
                <c:if test="${sessionScope.userrole == 'Administrator'}">
                    <a href="ManageCourse/form?idremove=${course.id}" class="remove"><img src="${url}images/remove.png" /></a>
                    <span class="info">course <strong>${course.name}</strong></span>
                </c:if>
            </td>
        </tr>
    </c:forEach>
</table>